<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/19
 * Time: 14:13
 */

/**
 * Created by wanglu on 2017/7/19.
 */
class Project extends AdminBase
{

    function __construct()
    {
        parent::__construct();
        $this->load->service('user_service');
        $this->load->service('project_service');
        $this->load->service('lang_service');
    }

    /**
     * 所有项目
     */
    public function projects()
    {
        $user_projects = $this->project_service->get_user_projects($this->loginUser);
        $this->showView("projects", array('projects' => $user_projects));
    }

    /**
     * 我的项目
     */
    public function myprojects()
    {
        $user_projects = $this->project_service->get_user_own_projects($this->loginUser);
        
        foreach ($user_projects as $key => $project) {
            $user_projects[$key]['project_role'] = AppConstants::PROJECT_ROLE_OWNER;
        }

        $this->showView("projects", array('projects' => $user_projects));
    }

    /**
     * 项目详细信息
     */
    public function detail()
    {
        $pid = $this->input->get("pid");
        $project = $this->project_service->get_project_by_id($pid);
        $project_owner = $this->user_service->get_user_by_id($project['owner_id']);
        $langs = $this->lang_service->get_langs_by_pid($pid);

        //项目 用户关系
        $project_user_relation = $this->project_service->get_project_user_relation($pid, $this->loginUser['id']);

        $this->showView("project_detail", array('project' => $project, 'owner' => $project_owner, 'project_user_relation' => $project_user_relation, 'langs' => $langs));
    }

    /**
     * 创建项目
     */
    public function add()
    {
        if ($this->is_post()) {
            $langs = $this->input->post("langs");
            $keys = $this->input->post("keys");
            $pname = $this->input->post("pname");
            $remark = $this->input->post("remark");

            $langs = json_decode($langs, true);
            $keys = json_decode($keys, true);
            $user = $this->session->userdata('user');
            if ($this->project_service->createProject($pname, $remark, $user['id'], $langs, $keys)) {
                die(json_encode(array("code" => 200)));
            } else {
                die(json_encode(array("code" => 201, "msg" => '操作失败')));
            }
        } else {
            $this->showView("add_project");
        }
    }

    public function delete() {
        $pid = $this->input->post("pid");
        if ($this->project_service->delete_project_by_id($pid)) {
            die(json_encode(array("code" => 200)));
        }  else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }
    }

    public function update() {

        $pid = $this->input->post("pid");
        $name = $this->input->post("name");
        $remark = $this->input->post("remark");
        if ($this->project_service->update_project($pid, $name, $remark)) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg" => '操作失败')));
        }



    }

        public function get() {
        $pid = $this->input->post("pid");
        $project = $this->project_service->get_project_by_id($pid);
        echo json_encode($project);
    }




}